Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / app / src / main / java / com / geeksville / mesh / worker / ServiceKeepAliveWorker.kt

Displaying Raw • Download

app/src/main/java/com/geeksville/mesh/worker/ServiceKeepAliveWorker.kt features/lockdown (1ea87dbb) Text, 3.68 KB

T8b949e/*
* Copyright (c) 2025-2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787com.geeksville.mesh.worker

Tff7b72import T7ee787android.app.Notification
Tff7b72import T7ee787android.content.Context
Tff7b72import T7ee787android.content.pm.ServiceInfo
Tff7b72import T7ee787android.os.Build
Tff7b72import T7ee787androidx.core.app.NotificationCompat
Tff7b72import T7ee787androidx.hilt.work.HiltWorker
Tff7b72import T7ee787androidx.work.CoroutineWorker
Tff7b72import T7ee787androidx.work.ForegroundInfo
Tff7b72import T7ee787androidx.work.WorkerParameters
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787com.geeksville.mesh.R
Tff7b72import T7ee787com.geeksville.mesh.service.MeshService
Tff7b72import T7ee787com.geeksville.mesh.service.startService
Tff7b72import T7ee787dagger.assisted.Assisted
Tff7b72import T7ee787dagger.assisted.AssistedInject
Tff7b72import T7ee787org.meshtastic.core.service.MeshServiceNotifications
Tff7b72import T7ee787org.meshtastic.core.service.SERVICE_NOTIFY_ID

T8b949e/**
* A worker whose sole purpose is to start the MeshService from the background. This is used as a fallback when
* `startForegroundService` is blocked by Android 14+ restrictions. It runs as an Expedited worker to gain temporary
* foreground start privileges.
*/
Tf0883e@HiltWorker
Tff7b72class T56d364ServiceKeepAliveWorker
Tf0883e@AssistedInject
Tff7b72constructorTb4b4b4(
Tf0883e@Assisted Te6edf3appContextTb4b4b4: Te6edf3ContextTb4b4b4,
Tf0883e@Assisted Te6edf3workerParamsTb4b4b4: Te6edf3WorkerParametersTb4b4b4,
Tff7b72private Tff7b72val Te6edf3serviceNotificationsTb4b4b4: Te6edf3MeshServiceNotificationsTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3CoroutineWorkerTb4b4b4(Te6edf3appContextTb4b4b4, Te6edf3workerParamsTb4b4b4) Tb4b4b4{

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffgetForegroundInfoTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3ForegroundInfo Tb4b4b4{
T8b949e// We use the same notification channel as the main service notification
T8b949e// to minimize user disruption.
T8b949e// On Android 12+, we need to provide a foreground info for expedited work.
Tff7b72val Te6edf3notification Tff7b72= Te6edf3createNotificationTb4b4b4(Tb4b4b4)

Tff7b72return Tff7b72if Tb4b4b4(Te6edf3BuildTb4b4b4.Te6edf3VERSIONTb4b4b4.Te6edf3SDK_INT Tff7b72>Tff7b72= Te6edf3BuildTb4b4b4.Te6edf3VERSION_CODESTb4b4b4.Te6edf3QTb4b4b4) Tb4b4b4{
Te6edf3ForegroundInfoTb4b4b4(
Te6edf3SERVICE_NOTIFY_IDTb4b4b4,
Te6edf3notificationTb4b4b4,
Te6edf3ServiceInfoTb4b4b4.Te6edf3FOREGROUND_SERVICE_TYPE_DATA_SYNC Te6edf3or Te6edf3ServiceInfoTb4b4b4.Te6edf3FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICETb4b4b4,
Tb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3ForegroundInfoTb4b4b4(Te6edf3SERVICE_NOTIFY_IDTb4b4b4, Te6edf3notificationTb4b4b4)
Tb4b4b4}
Tb4b4b4}

Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffTooGenericExceptionCaughtTa5d6ff"Tb4b4b4)
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffdoWorkTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Result Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffServiceKeepAliveWorker: Attempting to start MeshServiceTa5d6ff" Tb4b4b4}
Tff7b72return Tff7b72try Tb4b4b4{
Te6edf3MeshServiceTb4b4b4.Te6edf3startServiceTb4b4b4(Te6edf3applicationContextTb4b4b4)
Te6edf3ResultTb4b4b4.Te6edf3successTb4b4b4(Tb4b4b4)
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3ExceptionTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3eTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffServiceKeepAliveWorker failed to start serviceTa5d6ff" Tb4b4b4}
Te6edf3ResultTb4b4b4.Te6edf3failureTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Tff7b72private Tff7b72fun Td2a8ffcreateNotificationTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Notification Tb4b4b4{
T8b949e// We ensure channels are created
Te6edf3serviceNotificationsTb4b4b4.Te6edf3initChannelsTb4b4b4(Tb4b4b4)

T8b949e// We create a generic "Resuming" notification.
T8b949e// We use "my_service" which matches NotificationType.ServiceState.channelId in MeshServiceNotificationsImpl

Tff7b72return Te6edf3NotificationCompatTb4b4b4.Te6edf3BuilderTb4b4b4(Te6edf3applicationContextTb4b4b4, Ta5d6ff"Ta5d6ffmy_serviceTa5d6ff"Tb4b4b4)
Tb4b4b4.Te6edf3setSmallIconTb4b4b4(Te6edf3RTb4b4b4.Te6edf3drawableTb4b4b4.Te6edf3ic_launcher_foregroundTb4b4b4)
Tb4b4b4.Te6edf3setContentTitleTb4b4b4(Ta5d6ff"Ta5d6ffResuming Mesh ServiceTa5d6ff"Tb4b4b4)
Tb4b4b4.Te6edf3setPriorityTb4b4b4(Te6edf3NotificationCompatTb4b4b4.Te6edf3PRIORITY_LOWTb4b4b4)
Tb4b4b4.Te6edf3setOngoingTb4b4b4(Tff7b72trueTb4b4b4)
Tb4b4b4.Te6edf3buildTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.1 - Generated in 0.05s